Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Route refactor and fixes, CPython example #76

Merged
merged 9 commits into from
Jan 29, 2024

Conversation

michalpokusa
Copy link
Contributor

@michalpokusa michalpokusa commented Jan 25, 2024

This is a somewhat of a minor change, that does not affect users as it does not change the library's API.
While making on of my projects I noticed that on each request multiple Route objects are created and multiple re methods are called.

This PR changes the way Server handles it's routes. New method is more efficient and has some fixes compared to the current way.

⭐ Added:

  • Additinal validation to path in Route constructor
  • EDIT1: Example of using adafruit_httpserver with CPython (resolves add cpython example #77)

🗑️ Deleted:

  • _Routes class as it is no longer needed

🪛Fixes:

  • Literal dots in paths now behave correctly, before they matched every character
  • Before ... and .... could be used in other places than between slashes, now the error is raised when trying to do so

🛠️ Updated/Changed:

  • Route now has separate path and path_pattern, the later one is created using separate method for better readability
  • Route.matches had docs with typos, after the change docs were updated so it is no longer the case
  • EDIT1: Server.serve_forever defaults poll_interval to 0.1s and catches KeyboardInterrupts during sleeping, it also sleeps only if no request were processed, so it will not affect performance during bursts

🏗️ Refactor:

  • Now it is the Server's responsibility to find a matching Route
  • Only method and path is passed when finding a matching Route, this way we are not creating multiple unnecessary Route objects on every request
  • Example that shows manual static files handling is now more verbose, which resolves instructions error #75

@michalpokusa michalpokusa marked this pull request as draft January 28, 2024 15:18
@michalpokusa michalpokusa changed the title Route refactor and fixes Route refactor and fixes ,CPython example Jan 28, 2024
@michalpokusa michalpokusa changed the title Route refactor and fixes ,CPython example Route refactor and fixes, CPython example Jan 28, 2024
@michalpokusa michalpokusa marked this pull request as ready for review January 28, 2024 22:59
Copy link
Contributor

@FoamyGuy FoamyGuy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good to me. I tested a few examples successfully on a Feather ESP32-S3 TFT.

I also tested the new cpython example successfully.

I had to run it with sudo python in my environment for it to work, I was getting Permission Denied tracing back to socket.bind() without sudo. I'm not sure if that is intentional or if there is some way to lower the permission level needed. But it does make sense to me since it's binding to default port on the host PC to control traffic. Maybe not using 0.0.0.0 IP? (I tried 127.0.0.1 as well but it did not change behavior) or perhaps some port other than 80 could have lower permissions perhaps.

Thank you @michalpokusa and @tedder

@FoamyGuy FoamyGuy merged commit ec9b06f into adafruit:main Jan 29, 2024
1 check passed
adafruit-adabot added a commit to adafruit/Adafruit_CircuitPython_Bundle that referenced this pull request Jan 30, 2024
Updating https://github.com/adafruit/Adafruit_CircuitPython_Wiznet5k to 5.0.6 from 5.0.5:
  > Merge pull request adafruit/Adafruit_CircuitPython_Wiznet5k#125 from FoamyGuy/recv_timeout

Updating https://github.com/adafruit/Adafruit_CircuitPython_HTTPServer to 4.5.1 from 4.5.0:
  > Merge pull request adafruit/Adafruit_CircuitPython_HTTPServer#76 from michalpokusa/routes-refactor

Updating https://github.com/adafruit/Adafruit_CircuitPython_MiniMQTT to 7.5.8 from 7.5.6:
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#199 from vladak/loop_vs_keep_alive
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#200 from vladak/loop_timeout_vs_socket_timeout
  > Merge pull request adafruit/Adafruit_CircuitPython_MiniMQTT#184 from rjauquet/rej-fix-loop-blocking

Updating https://github.com/adafruit/Adafruit_CircuitPython_PIOASM to 1.0.0 from 0.8.2:
  > Merge pull request adafruit/Adafruit_CircuitPython_PIOASM#61 from Gebhartj/Gebhartj-patch-1

Updating https://github.com/adafruit/Adafruit_CircuitPython_Bundle/circuitpython_library_list.md to NA from NA:
  > Updated download stats for the libraries
@michalpokusa michalpokusa deleted the routes-refactor branch February 7, 2024 06:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

instructions error
2 participants